//
//  ZYLoadMoreViewModel.m
//  YiLin
//
//  Created by Zyark on 2018/6/14.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYLoadMoreViewModel.h"
#import "AFNetWorkUtils.h"

@implementation ZYLoadMoreViewModel
-(instancetype)init{
    if(self = [super init]){
        self.params = [NSMutableDictionary new];
    }
    return self;
}


-(RACCommand *)sourceCommand{
    if(!_sourceCommand){
        @weakify(self);
        _pageNum = 1;
        _sourceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            return [[[AFNetWorkUtils racGETWithURL:self.url params:_params] map:^id(id value) {
                NSDictionary *data = value;
                if([self.url hasSuffix:ZYAPI_GET_SYSTEM_MESSAGE_URL] || [self.url hasSuffix:ZYAPI_GET_USER_MESSAGE_URL]){
                    NSArray *row = [data objectForKey:@"result"];
                    if(!row){
                        return [NSMutableArray array];
                    }
                    return [NSArray modelArrayWithClass:_clazz json:row];
                }else{
                    NSDictionary *result = [data objectForKey:@"result"];
                    NSArray *row = [result objectForKey:@"rows"];
                    if(!row){
                        return [NSMutableArray array];
                    }
                    return [NSArray modelArrayWithClass:_clazz json:row];
                }
            }] map:^id(NSMutableArray *value) {
                if (![self.sourceArray count]) {
                    return [self firstLoad:value];
                }
                //2，上拉加载更多
                if (self.isLoadMore) {
                    return [self pullUpLoadMore:value];
                }
                //3，下拉刷新
                return [self pullDownLoadMore:value];
            }];
            return [[[AFNetWorkUtils racGETWithURL:self.url params:_params class:_clazz] map:^id(id value) {
                NSDictionary *data = value;
                NSDictionary *result = [data objectForKey:@"result"];
                NSArray *row = [result objectForKey:@"rows"];
                if(!row){
                    return [NSMutableArray array];
                }
                return row;
            }] map:^id(NSMutableArray *value) {
                if (![self.sourceArray count]) {
                    return [self firstLoad:value];
                }
                //2，上拉加载更多
                if (self.isLoadMore) {
                    return [self pullUpLoadMore:value];
                }
                //3，下拉刷新
                return [self pullDownLoadMore:value];
            }];
        }];
    }
    return _sourceCommand;
}

-(void)loadNewData{
    _isLoadMore = NO;
    _pageNum = 1;
    [_sourceArray removeAllObjects];
    [self.params setObject:[NSString stringWithFormat:@"%d",1] forKey:@"page"];
    [self.sourceCommand execute:nil];
}

- (void)loadNextPageData{
    _pageNum +=1;
    _isLoadMore = YES;
    [self.params setObject:[NSString stringWithFormat:@"%ld",_pageNum] forKey:@"page"];
    [self.sourceCommand execute:nil];
}

/**
 *  第一次加载
 */
- (NSMutableArray *)firstLoad:(NSMutableArray *)array {
    self.sourceArray = array;
    return self.sourceArray;
}

/**
 *  上拉加载
 */
- (NSMutableArray *)pullUpLoadMore:(NSMutableArray *)array {
    //    if(![array count]){
    //        _pageNum -- ;
    //    }
    [self.sourceArray addObjectsFromArray:array];
    return self.sourceArray;
}

/**
 *  下拉刷新
 */
- (NSMutableArray *)pullDownLoadMore:(NSMutableArray *)array {
    //    NSInteger prevoiusCount = self.sourceArray.count;
    [array enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
        if ([object isEqual:self.sourceArray[0]]) {
            *stop = YES;
            NSArray * subArray = [array subarrayWithRange:NSMakeRange(0, idx)];
            if (!subArray || [subArray count]) {
                NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [subArray count])];
                [self.sourceArray insertObjects:subArray atIndexes:indexSet];
            }
        }
    }];
    return self.sourceArray;
}
@end
